﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using JM.JCY.Proxy;
using JM.JCY.BusinessEntity;
using JM.JCY.Common;
using Microsoft.SharePoint;
using System.Web;

namespace JM.JCY.SharePointComponent.Announcement.ViewAnnounceDetail
{
    public partial class ViewAnnounceDetailUserControl : UserControl
    {
        public int _announceId
        {
            set { ViewState["_announceId"] = value; }
            get { return (int)ViewState["_announceId"]; }
        }

        public string _attachmentUrl
        {
            set { ViewState["_attachmentUrl"] = value; }
            get { return (string)ViewState["_attachmentUrl"]; }
        }

        public string _attachmentName
        {
            set { ViewState["_attachmentName"] = value; }
            get { return (string)ViewState["_attachmentName"]; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string currentAccount = SPContext.Current.Web.CurrentUser.Name;
                if (Request.QueryString["AnnounceId"] != null)
                {
                    _announceId = Int32.Parse(Request.QueryString["AnnounceId"]);
                }

                using (AnnounceProxy proxy = new AnnounceProxy())
                {
                    AnnounceDataSet.T_AnnounceDataTable announceDT = proxy.GetAnnounce(_announceId, string.Empty, string.Empty);

                    if (announceDT.Rows.Count > 0)
                    {
                        AnnounceDataSet.T_AnnounceRow annouceRow = announceDT[0];
                        this.lbSubject.Text = annouceRow.Subject;
                        this.lblSummary.Text = annouceRow.Summary;
                        this.lblCreatedDate.Text = annouceRow.CREATED_TIME.ToString(JCYConstant.Date_Format_CN);

                        // Add by Hong Liang 2012-05-02
                        if (!annouceRow.IsAttachmentUrlNull())
                        {
                            _attachmentUrl = annouceRow.AttachmentUrl;
                            _attachmentName = annouceRow.AttachmentName;
                            this.lkbAttach.Text = _attachmentName;
                        }

                        //update status to Read
                        proxy.UpdateAnnounceStatus(_announceId, currentAccount);

                    }

                }
            }
        }

        // Add by Hong Liang 2012-05-02
        public void OpenAttachment(string url, string fileName)
        {
            try
            {
                fileName = HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8);

                SPWeb web = SPContext.Current.Web;
                web.AllowUnsafeUpdates = true;

                SPFile tempFile = web.GetFile(url);

                byte[] obj = (byte[])tempFile.OpenBinary();
                HttpContext.Current.Response.ClearContent();
                HttpContext.Current.Response.ClearHeaders();
                HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment; filename= " + fileName);
                HttpContext.Current.Response.ContentType = "application/octet-stream";

                //Check that the client is connected and has not closed the connection after the request

                if (HttpContext.Current.Response.IsClientConnected)
                    HttpContext.Current.Response.BinaryWrite(obj);

                HttpContext.Current.Response.Flush();
                HttpContext.Current.Response.Close();

                web.AllowUnsafeUpdates = false;
            }

            catch (Exception ex)
            {
                Response.Write(ex.ToString());
            }
        }

        protected void lkbAttach_Click(object sender, EventArgs e)
        {
            OpenAttachment(SPContext.Current.Web.ServerRelativeUrl + _attachmentUrl, _attachmentName);
        }
    }
}
